package org.estela.vm;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.estela.comp.Expression;
import org.estela.comp.Function;
import org.estela.vm.types.VoidType;

public class Memory {

	/** Functions table */
	private Map<String,Instruction[]> _functions = new HashMap<String,Instruction[]>();
	
	/**
	 * Performs a gc operation.
	 * 
	 * @return Returns the gc operation time
	 */
	public long gc() {
		long t0 = System.currentTimeMillis();
		
		return System.currentTimeMillis() - t0;
	}
	
	public void addFunction(String functionName, Instruction[] instructions) {
		_functions.put(functionName, instructions);
	}
	
	/**
	 * Returns a function given its name.
	 * 
	 * @param functionName The function name
	 * @return Returns a function given its name
	 */
	public Instruction[] getFunction(String functionName) {
		return _functions.get(functionName);
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private Map<String, Reference> staticMemory = new HashMap<String, Reference>();
	
	private Map<String, Reference> heap = new HashMap<String, Reference>();
	
	//private Map<String, >
	
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	/**
	 * Reset memory.
	 * 
	 * 
	 */
	public void reset() {
		_functions.clear();

		Function outFunction = new Function("out", new Class[]{Expression.class}, VoidType.class);
		_functions.put(outFunction.getName(), new Instruction[]{
			new Instruction(Instruction.PRINT)
		});

	}
}
